package com.warmlight.voicepacket;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.warmlight.voicepacket.activity.VideoDetailsActivity;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.http.HttpUtils;
import com.warmlight.voicepacket.http.ResponseData;
import com.warmlight.voicepacket.plugin.QmzbMediaManager;
import com.warmlight.voicepacket.plugin.QmzbMediaView;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.SPUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class SplashActivity extends BaseActivity {

    private static long WAITING = 2000;
    private QmzbMediaView media;
    private TextView tv_jump;
    private TextView tv_bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        Intent intent = getIntent();
        Uri data = intent.getData();

        initView();
        if (!isTaskRoot()){
            if (data != null) {
                jump(data);
            }else {

            }
            finish();
        }else {
            if (data != null) {
                jump(data);
            }else {
                if (TextUtils.equals("1",SPUtils.getString(SPUtils.SPLASH_VIDEO))){
                    mHandler.sendEmptyMessageDelayed(0, WAITING);
                }else {
                    loadVideo();
                }

//
            }
        }

    }

    private void initView() {
        media = findViewById(R.id.media);
        tv_jump = findViewById(R.id.tv_jump);
        tv_bg = findViewById(R.id.tv_bg);

        tv_bg.setVisibility(View.VISIBLE);
        media.setVisibility(View.GONE);
        tv_jump.setVisibility(View.GONE);

        tv_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                SplashActivity.this.finish();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        QmzbMediaManager.instance().releaseMediaPlayer();
    }

    @Override
    public void onPause() {
        super.onPause();
        QmzbMediaManager.pause();
    }

    private void loadVideo(){
        String url = HttpConfig.APP_VIDEO;
        TreeMap<String,String> treeMap = new TreeMap<>();
        HttpClient.getInstance().postRxAndroidAES(url,treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (HttpUtils.isCodeSuccess(responseData.getCode())){
                            String data = responseData.getData();
                            ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(data);
                            if (listMapByJson.size() >  0){
                                String url1 = listMapByJson.get(0).get("url");
                                playVideo(url1);
                            }else{
                                mHandler.sendEmptyMessageDelayed(0, WAITING);
                            }
                        }else{
                            mHandler.sendEmptyMessageDelayed(0, WAITING);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mHandler.sendEmptyMessageDelayed(0, WAITING);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void playVideo(String url) {
        media.setVisibility(View.VISIBLE);
        tv_jump.setVisibility(View.VISIBLE);
        tv_bg.setVisibility(View.GONE);
        SPUtils.putString(SPUtils.SPLASH_VIDEO,"1");
        media.prepareVideo(url);
        media.setControlHidden(true);
        media.setOnComPletion(new QmzbMediaView.OnComPletion() {
            @Override
            public void onCompletion() {
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                SplashActivity.this.finish();
            }
        });
    }

    private void jump(Uri data){
        //voicepacket://voicepacket.com?pageName=PostDetailPage&id=342&
        // otherPara={"otherPara":{"id":"342","category":2,"video_width":852,"video_height":480}}
        String id = data.getQueryParameter("id");
        String pageName = data.getQueryParameter("pageName");
        if (TextUtils.equals("PostDetailPage",pageName)){
            String otherPara = data.getQueryParameter("otherPara");
            try {
                JSONObject jsonObject = new JSONObject(otherPara);
                JSONObject otherPara1 = jsonObject.optJSONObject("otherPara");
                String category = otherPara1.optString("category");


                if (TextUtils.equals("1",category)){
                    jumpPagePost(1,id,0);
                }else if (TextUtils.equals("2",category)){
                    String video_width = otherPara1.optString("video_width");
                    String video_height = otherPara1.optString("video_height");
                    int w = Integer.valueOf(video_width);
                    int h = Integer.valueOf(video_height);
                    if (w > h){
                        jumpPagePost(2,id,1);
                    }else{
                        jumpPagePost(2,id,2);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                mHandler.sendEmptyMessageDelayed(0, WAITING);
            }
        }else{
            mHandler.sendEmptyMessageDelayed(0, WAITING);
        }

    }

    private void jumpPagePost(int type, final String id, final int wh){
        //图文
        if (type == 1) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    VideoDetailsActivity.startVideoDetailsActivity(getContext(),id,VideoDetailsActivity.VIDEO_PICTURE,null,"");
                    finish();
                }
            },WAITING);

        }else if (type ==2){
            //视频
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (wh == 1){
                        VideoDetailsActivity.startVideoDetailsActivity(getContext(),id,VideoDetailsActivity.VIDEO_HORIZONTAL,null,"");
                    }else{
                        VideoDetailsActivity.startVideoDetailsActivity(getContext(),id,VideoDetailsActivity.VIDEO_VERTICAL,null,"");
                    }
                    finish();
                }
            },WAITING);

        }
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));
                    SplashActivity.this.finish();
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }
}

